package main

import (
	"fmt"
	"os"

	"github.com/spf13/viper"
)

type KeyPairConfig struct {
	Format      string `mapstructure:"format"`
	HyPublicKey string `mapstructure:"hy-public-key"`
	PrivateKey  string `mapstructure:"private-key"`
}

//定义config结构体
type Config struct {
	Listen  string        `mapstructure:"listen"`
	Sm2Keys KeyPairConfig `mapstructure:"sm2"`
	RsaKeys KeyPairConfig `mapstructure:"rsa"`
}

func ReadConfig() *Config {
	config := viper.New()
	//config.AddConfigPath("./")

	path, _ := os.Getwd()
	config.AddConfigPath(path)

	config.SetConfigName("config")
	config.SetConfigType("json")

	if err := config.ReadInConfig(); err != nil {
		panic(err)
	}

	//fmt.Printf("config: %v\n", config.AllSettings())

	//直接反序列化为Struct
	var configjson Config
	if err := config.Unmarshal(&configjson); err != nil {
		fmt.Println(err)
	}

	return &configjson
}
